import 'package:flutter/cupertino.dart';
import 'package:flutter_one/class/app/news/m/NewsInfoModel.dart';
import 'package:flutter_one/class/appbase/BaseViewModel.dart';
import 'package:flutter_one/class/utils/CMRequest.dart';
import '../m/NewsTypeModel.dart';
import 'package:flutter_one/class/appsetting/setting.dart' deferred as setting;
import 'package:flutter_one/class/extension/ProgressHudExtension.dart';
import 'package:flutter_one/class/app/news/m/NewsTypeModel.dart';

/*资讯VM*/
class NewsPageViewModel extends BaseViewModel {
  NewsPageViewModel() : super();

  //MARK: - 栏目
  // 栏目分类
  final List<NewsTypeModel> arrCategory = <NewsTypeModel>[];

  // 加载栏目类别
  void categoryLoad(BuildContext context,
      {bool isLoading = false,
      void Function(bool isOk, String msg)? finishBack}) {
    _categoryLoad(context, isLoading: isLoading, finishBack: finishBack);
  }

  //MARK: - 列表
  // 加载列表
  void listLoad(BuildContext context, int classId, int pageIndex,
      {bool isLoading = false,
      required void Function(
              bool isOk, String msg, List<NewsInfoModel>? arrList,int size)
          finishBack}) {
    _listLoad(context, classId, pageIndex,
        isLoading: isLoading, finishBack: finishBack);
  }
}

//MARK: - 栏目
// 栏目
extension on NewsPageViewModel {
  // 加载全部栏目
  Future<void> _categoryLoad(BuildContext context,
      {bool isLoading = false,
      void Function(bool isOk, String msg)? finishBack}) async {
    setState(ViewState.Busy);

    await setting.loadLibrary();

    final String strUrl = "${setting.K_APP_REQUEST_HOST}api/news/class/list";
    CMRequest.shareInstance.getRequestForServerData(strUrl,
        successBack: (Object responseData) async {
      arrCategory.clear(); //清除

      for (var json in (responseData as List)) {
        final model = NewsTypeModel.fromJson(json);
        arrCategory.add(model);
      }

      setState(ViewState.Idle);
      if (finishBack != null) {
        finishBack!(true, '请求成功');
      }
    }, failureBlock: (String responseString) async {
      ProgressHudExtension.showErrorDialog(responseString, context);
      setState(ViewState.Idle);
      if (finishBack != null) {
        finishBack!(false, responseString);
      }
    });
  }
}

//MARK: - 资讯列表
// 资讯列表
extension on NewsPageViewModel {
  //加载列表
  void _listLoad(BuildContext context, int cId, int pageIndex,
      {bool isLoading = false,
      required void Function(
              bool isOk, String msg, List<NewsInfoModel>? arrList,int size)
          finishBack}) {
    //参数
    final dicParams = <String, String>{
      "pageNum": "$pageIndex", //首页默认1
      "pageSize": "${setting.K_APP_PAGE_SIZE}",
      "classId": "$cId"
    };

    String strUrl = '${setting.K_APP_REQUEST_HOST}api/news/info/list';
    if (cId == 5) {
      //快讯
      strUrl = '${setting.K_APP_REQUEST_HOST}api/news/info/apiGetHomeList';
    }

    CMRequest.shareInstance.getRequestForServerData(strUrl,
        mapParams: dicParams, successBack: (Object responseData) async {
      List<NewsInfoModel> arrList = <NewsInfoModel>[];
      for (var json in (responseData as List)) {
        final model = NewsInfoModel.fromJson(json);
        arrList.add(model);
      }

      finishBack(true, '加载成功', arrList,setting.K_APP_PAGE_SIZE);
    }, failureBlock: (String responseString) async {
      ProgressHudExtension.showErrorDialog(responseString, context);
      finishBack(false, responseString, null,setting.K_APP_PAGE_SIZE);
    });
  }
}
